// 口腔健康影响程度量表(OHIP-14)逻辑处理文件
import { ohip14Config } from '../config/ohip14Config.js';

/**
 * 计算OHIP-14评估结果
 * @param {Object} formData - 表单数据
 * @returns {Object} 评估结果
 */
export function calculateOHIP14Assessment(formData) {
    let totalScore = 0;
    const questionScores = {};

    // 计算每个问题的得分并累加总分
    for (let i = 1; i <= 14; i++) {
        const questionId = `Q${String(i).padStart(2, '0')}`;
        const score = parseInt(formData[questionId]) || 0;
        questionScores[questionId] = score;
        totalScore += score;
    }

    // 根据总分确定影响程度
    let diagnosis = '';
    let explanation = '';

    if (totalScore === 0) {
        diagnosis = '口腔健康影响程度：无影响';
        explanation = '您的口腔健康状况对生活质量没有产生明显影响，继续保持良好的口腔卫生习惯。';
    } else if (totalScore <= 14) {
        diagnosis = '口腔健康影响程度：轻度影响';
        explanation = '您的口腔健康状况对生活质量产生了轻度影响。建议定期进行口腔检查，注意口腔卫生，必要时寻求专业治疗。';
    } else if (totalScore <= 28) {
        diagnosis = '口腔健康影响程度：中度影响';
        explanation = '您的口腔健康状况对生活质量产生了中度影响。建议尽快进行口腔检查和治疗，改善口腔健康状况。';
    } else if (totalScore <= 42) {
        diagnosis = '口腔健康影响程度：重度影响';
        explanation = '您的口腔健康状况对生活质量产生了重度影响。建议立即寻求专业口腔治疗，并采取积极的口腔保健措施。';
    } else {
        diagnosis = '口腔健康影响程度：极重度影响';
        explanation = '您的口腔健康状况对生活质量产生了极重度影响。建议立即就医，接受全面的口腔健康评估和系统治疗，以改善生活质量。';
    }

    return {
        totalScore,
        maxScore: 56,
        diagnosis,
        explanation,
        questionScores,
        // 提供详细的问题得分信息
        details: {
            questions: Object.keys(questionScores).map(key => ({
                id: key,
                label: getQuestionLabel(key),
                score: questionScores[key]
            }))
        }
    };
}

/**
 * 获取问题标签
 * @param {string} questionId - 问题ID
 * @returns {string} 问题标签
 */
function getQuestionLabel(questionId) {
    const question = ohip14Config.formGroups[0].questions.find(q => q.id === questionId);
    return question ? question.label : questionId;
}

/**
 * 生成OHIP-14评估报告
 * @param {Object} assessmentResult - 评估结果
 * @param {Object} formData - 表单数据
 * @returns {Object} 评估报告
 */
export function generateOHIP14Report(assessmentResult, formData) {
    const report = {
        basicInfo: {
            patientName: formData.patientName,
            age: formData.age,
            gender: formData.gender,
            recordTime: formData.recordTime
        },
        summary: {
            totalScore: assessmentResult.totalScore,
            maxScore: assessmentResult.maxScore,
            diagnosis: assessmentResult.diagnosis,
            explanation: assessmentResult.explanation
        },
        details: assessmentResult.details,
        timestamp: new Date().toISOString()
    };

    return report;
}

/**
 * 验证OHIP-14表单数据
 * @param {Object} formData - 表单数据
 * @returns {Object} 验证结果
 */
export function validateOHIP14Form(formData) {
    const errors = [];
    const warnings = [];

    // 个人信息字段已改为选填，不再进行必填验证
    // 只验证如果填写了年龄，则必须在有效范围内
    if (formData.age !== null && formData.age !== undefined && formData.age !== '') {
        const age = parseInt(formData.age);
        if (isNaN(age) || age < 0 || age > 150) {
            errors.push('年龄应在0-150岁之间');
        }
    }

    // 验证所有问题是否已回答
    for (let i = 1; i <= 14; i++) {
        const questionId = `Q${String(i).padStart(2, '0')}`;
        if (formData[questionId] === undefined || formData[questionId] === null || formData[questionId] === '') {
            errors.push(`请回答第${i}题`);
        }
    }

    return {
        isValid: errors.length === 0,
        errors,
        warnings,
        hasWarnings: warnings.length > 0
    };
}

export default {
    calculateOHIP14Assessment,
    generateOHIP14Report,
    validateOHIP14Form
};

